Cancelled
Push — release/2.1.0 ( 9888ea )
by Kevin Van
09:55 queued 09:52
created

Ranking.render   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 1
1
import React, { Fragment } from "react"
2
import { graphql, useStaticQuery } from "gatsby"
3
4
import "./Ranking.scss"
5
6
class Ranking extends React.Component<RankingProps, RankingState> {
7
  public static defaultProps = {
8
    teamId: 1,
9
  }
10
11
  teamId: number
12
  kcvvPsdApi: string
13
14
  constructor(props: RankingProps) {
15
    super(props)
16
17
    this.state = {
18
      data: [],
19
      loading: true,
20
    }
21
22
    this.kcvvPsdApi = ``
23
    this.teamId = props.teamId
24
  }
25
26
  updateData(): void {
27
    if (this.teamId === null || this.kcvvPsdApi === ``) {
28
      return
29
    }
30
31
    const apiUrl = `${this.kcvvPsdApi}/ranking/${this.teamId}`
32
33
    fetch(apiUrl)
34
      .then((response) => response.json())
35
      .then((json) => this.setState({ data: json, loading: false }))
36
  }
37
38
  componentDidMount(): void {
39
    this.updateData()
40
  }
41
42
  renderRanking = (ranking: RankingDataObject, i: number): JSX.Element => {
43
    return (
44
      <div className={`ranking`} key={i}>
45
        <h4>{ranking.name.replace(`Voetbal : `, ``)}</h4>
46
        <table>
47
          <thead>
48
            <tr>
49
              <th className={`table__column__number`}>#</th>
50
              <th className={`table__column__string`}>Team</th>
51
              <th className={`table__column__number show-for-medium`}>P</th>
52
              <th className={`table__column__number`}>W</th>
53
              <th className={`table__column__number`}>D</th>
54
              <th className={`table__column__number`}>L</th>
55
              <th className={`table__column__number show-for-medium`}>G+</th>
56
              <th className={`table__column__number show-for-medium`}>G-</th>
57
              <th className={`table__column__number`}>+/-</th>
58
              <th className={`table__column__number`}>Pts</th>
59
            </tr>
60
          </thead>
61
          <tbody>
62
            {ranking.teams.map((team: RankingDataTeamObject, j: number) => (
63
              <tr key={j}>
64
                <td className={`table__column__number`}>{team.rank || `-`}</td>
65
                <td
66
                  className={`table__column__string ${
67
                    team.team?.club?.localName?.toLowerCase().includes(`elewijt`) ? `table__column--highlight` : ``
68
                  }`}
69
                >
70
                  {team.team?.club?.localName || ``}
71
                </td>
72
                <td className={`table__column__number show-for-medium`}>{team.matchesPlayed || `0`}</td>
73
                <td className={`table__column__number`}>{team.wins || `0`}</td>
74
                <td className={`table__column__number`}>{team.draws || `0`}</td>
75
                <td className={`table__column__number`}>{team.losses || `0`}</td>
76
                <td className={`table__column__number show-for-medium`}>{team.goalsScored || `0`}</td>
77
                <td className={`table__column__number show-for-medium`}>{team.goalsConceded || `0`}</td>
78
                <td className={`table__column__number`}>{team.goalsScored - team.goalsConceded || `0`}</td>
79
                <td className={`table__column__number`}>{team.points || `0`}</td>
80
              </tr>
81
            ))}
82
          </tbody>
83
        </table>
84
      </div>
85
    )
86
  }
87
88
  renderRankings = (): JSX.Element => {
89
    if (this.state.loading === false && this.state.data) {
90
      const rankings = this.state.data
91
      return (
92
        <Fragment>
93
          {rankings
94
            .sort((a, b) => a.name.localeCompare(b.name))
95
            .map((ranking: RankingDataObject, i: number) => {
96
              return this.renderRanking(ranking, i)
97
            })}
98
        </Fragment>
99
      )
100
    } else {
101
      return <div>Loading...</div>
102
    }
103
  }
104
105
  render(): JSX.Element {
106
    const config: RankingData = useStaticQuery(graphql`
107
      query {
108
        site {
109
          siteMetadata {
110
            kcvvPsdApi
111
          }
112
        }
113
      }
114
    `)
115
116
    this.kcvvPsdApi = config.site.siteMetadata.kcvvPsdApi
117
118
    return <section className={`ranking__wrapper`}>{this.renderRankings()}</section>
119
  }
120
}
121
122
export default Ranking
123